<?php
/*
 * 资讯管理
 */
class ArchiveAction extends CommonAction{
    
    /*
     * 列表
     */
    public function index(){
        $prefix = C('DB_PREFIX');
        $m = M('archive');
        /*搜索*/
        if($title = I('get.title',0)){$map['title']=array('like','%'.$title.'%');}
        if($order = I('get.order',0)){$orderby=$order.' desc';}else{$orderby="id desc";}
        /*如果带有typeid参数，则查询出这个栏目所属的模型*/
        if($typeid = I('get.typeid',0)){
            $map[$prefix.'arctype.id'] = $typeid;
            $m2 = M('arctype');
            $channel = $m2->field($prefix.'arctype.*,'.$prefix.'channel.modeltype as modeltype,'.$prefix.'channel.tablename as tablename')->join($prefix.'channel on '.$prefix.'.channel.id = '.$prefix.'arctype.channelid')->where(array($prefix.'arctype.id'=>$typeid))->find();
        }
        //当为独立模型时，只查询独立模型一张表
        if($channel['modeltype'] == 3){//独立模型
            $m3 = M($channel['tablename']);
            $count = $m3->field($prefix.$channel['tablename'].'.*,'.$prefix.'arctype.typename')
            ->join($prefix.'arctype on '.$prefix.'arctype.id='.$prefix.$channel['tablename'].'.typeid')->where($map)
            ->count();
            $p = new Page($count,10);
             $list = $m3->field($prefix.$channel['tablename'].'.*,'.$prefix.'arctype.typename,'.$prefix.'arctype.channelid')
            ->join($prefix.'arctype on '.$prefix.'arctype.id='.$prefix.$channel['tablename'].'.typeid')
            ->limit("$p->firstRow,$p->listRows")->order($orderby)->where($map)
            ->select();
        }else{//系统模型和自动模型都要使用主表，列表页只调用主表信息
            $count = $m->field($prefix.'archive.*,'.$prefix.'arctype.typename')
            ->join($prefix.'arctype as '.$prefix.'arctype on '.$prefix.'arctype.id='.$prefix.'archive.typeid')->where($map)
            ->count();            
            $p = new Page($count,10);
            $list = $m->field($prefix.'archive.*,'.$prefix.'arctype.typename,'.$prefix.'arctype.channelid')
            ->join($prefix.'arctype on '.$prefix.'arctype.id='.$prefix.'archive.typeid')
            ->limit("$p->firstRow,$p->listRows")->order($orderby)->where($map)
            ->select();
        }
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $m = M('arctype');
        $type = $m->order('topid asc,id desc')->select();
        $this->assign('type',  Tool::tree($type));
        $this->display();
    }
    
    /*
     * 资讯增加
     */
    public function add(){
        /*增加资讯前，首先查出进入时有没有带栏目ID，如果有，就查出栏目属性什么模型，根据模型表来给出模板*/
        $m = D('Archive');
        $m2 = D('arctype');
        $prefix = C('DB_PREFIX');        
        $typeid = I('get.typeid',0); 
        
        if($typeid){
            $type = $m2->field($prefix.'arctype.*,'.$prefix.'channel.tablename as tablename,'.$prefix.'channel.modeltype as modeltype')
                    ->join($prefix.'channel on '.$prefix.'channel.id = '.$prefix.'arctype.channelid')
                    ->where(array($prefix.'arctype.id'=>$typeid))
                    ->find();
         }else{$type['tablename']= 'archivebody';}
        if($type['modeltype'] == 3){//独立模型
            $temp = 'table_'.$type['tablename'];
        }elseif($type['tablename'] == 'archivebody'){//系统模型
            $temp = 'archive_'.$type['tablename'];
        }else{//自动模型
             $temp = 'archive_'.$type['tablename'];
        }
        /*表单提交保存增加*/
        if(IS_POST){
            
            $m3 = D(ucwords($type['tablename']));
            if($type['modeltype'] == 3){//独立模型表的插入
                if(false === $m3->create()){$this->error($m->getError());}
                if(false === $m3->add()){$this->error('添加失败');}
                $this->success('添加成功');exit();
            }
            if(false === $m->create()){$this->error($m->getError());}
            //上传缩略图
                if(!empty($_FILES['picname']['name'])) {
                    $info=$this->_upload();
                    if(!empty($_FILES['picname']['name'])){
                        $savename=$info['picname']['savename'];
                        $img = C("UPLOADS").$savename;
                        $m->picname = $img;
                    }
                }
            if(false === $id = $m->add()){$this->error('文章主表添加失败');}
            if(false === $m3->create()){$this->error($m->getError());}
            if($type['modeltype'] != 3){$m3->aid = $id;}            
            if(false === $m3->add()){$this->error('文章副表添加失败');}
            $this->success('添加成功');exit();
        }        
        $list = $m2->order('topid asc,id desc')->select();
		$arr['typeid'] = $_GET['typeid'];
		$this->assign('arr',$arr);
        $this->assign('list',  Tool::tree($list));
        $this->display($temp);
    }
    
    /*
     * 资讯修改
     */
    public function edit($id){
        if(!is_zhengshu($_GET['channelid'])){exit('非法的提交');}
        $prefix = C('DB_PREFIX');  
        /*刚打开时显示修改的文章,判断文章模型表*/
        $m3 = M('channel');
        $channel = $m3->find($_GET['channelid']);
        $m4 = M('archive');
        //模型判断
        if($channel['modeltype'] == 3){//独立模型
            $m5 = M($channel['tablename']);
            $arr = $m5->find($id);
            $temp = 'table_'.$channel['tablename'];
        }elseif($channel['tablename'] == 'archivebody'){//系统模型
            $arr = $m4->join($prefix.$channel['tablename'].' on '.$prefix.$channel['tablename'].'.aid = '.$prefix.'archive.id')->where(array('id'=>$id))->find();
           $temp = 'archive_'.$channel['tablename']; 
        }else{//自动模型
             $arr = $m4->join($prefix.$channel['tablename'].' on '.$prefix.$channel['tablename'].'.aid = '.$prefix.'archive.id')->where(array('id'=>$id))->find();
             $temp = 'archive_'.$channel['tablename'];
        }
        /*提交修改保存更新*/
        if(IS_POST){
            //模型判断
            if($channel['modeltype'] == 3){//独立模型
               $m5 = M($channel['tablename']);
               if(false === $m5->create()){$this->error($m5->getError());}
               if(false === $m5->save()){$this->error('修改失败');}
               $this->success('修改成功');exit();
            }elseif($channel['tablename'] == 'archivebody'){//系统模型
                $m = D('Archive');
                $m2 = D('archivebody');
                if(false === $m->create()){$this->error($m->getError());}
                //上传缩略图
                if(!empty($_FILES['picname']['name'])) {
                    $info=$this->_upload();
                    if(!empty($_FILES['picname']['name'])){
                        $savename=$info['picname']['savename'];
                        $img = C("UPLOADS").$savename;
                        $m->picname = $img;
                    }
                }
                if(false === $m->save()){$this->error('修改主表失败');}
                if(false === $m2->create()){$this->error($m2->getError());}
                if(false === $m2->where(array('aid'=>$id))->save()){$this->error('修改副表失败22');}
                $this->success('修改成功');exit();
            }else{//自动模型
                $m = D('archive');
                $m6 = D(ucwords($channel['tablename']));
                if(false === $m->create()){$this->error($m->getError());}
                if(false === $m->save()){$this->error('修改主表失败');}
                if(false === $m6->create()){$this->error($m6->getError());}
                if(false === $m6->save()){echo $m6->_sql();dump($m6);exit;$this->error('修改副表失败');}
                $this->success('修改成功');exit();
            }
        }
        /*显示模板及变量*/
        $this->assign('arr',$arr);
        $m2 = M('arctype');
        $type = $m2->find($arr['typeid']);
        $list = $m2->field($prefix.'arctype.*')
            ->join($prefix.'channel on '.$prefix.'channel.id='.$prefix.'arctype.channelid')
            ->where(array($prefix.'channel.tablename'=>$channel['tablename']))
            ->order('topid asc,id desc')->select();
        $this->assign('list',  Tool::tree($list));
        $this->assign('edit',  1);
        $this->assign('channel', $channel);
        $this->assign('type', $type);
        $this->display($temp);
    }
    
    /*
     * 资讯删除
     */
    public function del($id){
        $m = M('archive');
        $m2 = M('archivebody');
        if($m->delete($id)){
            $m2->delete($id);
            $this->success('删除成功');exit();}
        $this->error('删除失败');
    }
    
    /*
     * 删除选中的资讯
     */
    public function delall(){
        $m = M('archive');
        $m2 = M('archivebody');
        if($_GET['id']){
            foreach($_GET['id'] as $v){
                $m->delete($v);
                $m2->delete($v);
            }
        }
        $this->success('删除成功');exit();
    }
}